﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RealtorsPortal.Business_Logic_Layer;
using RealtorsPortal.Entities;
using RealtorsPortal.Data_Access_Layer;
using System.Data;

namespace RealtorsPortal
{
    public partial class Home : System.Web.UI.MasterPage
    {
        CategoriesDAL ctd = new CategoriesDAL();
        RoleDAL rd = new RoleDAL();
        AdvertisementsDAL avdd = new AdvertisementsDAL();
        CitiesDAL cd = new CitiesDAL();
        DistrictsDAL dtd = new DistrictsDAL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["adminLogin"] != null && Session["adminRole"] != "Administrator")
                {
                    taskbar_right.Visible = false;
                    LoggedPanel.Visible = true;
                    lblUserName.Text = Session["adminLogin"].ToString();
                }
                this.LoadAndBindData();
            }
        }

        private void LoadAndBindData()
        {
            //categories
            ddlCategories.DataSource = rptMenu.DataSource = ctd.GetCategoriessData();
            rptMenu.DataBind();
            ddlCategories.DataTextField = "CategoryName";
            ddlCategories.DataValueField = "CategoryId";
            ddlCategories.DataBind();
            ddlCategories.Items.Insert(0, new ListItem("---Choose all----", "0"));

            //roles
            ddlRoles.DataSource = rd.GetRoleDataTable();
            ddlRoles.DataTextField = "RoleName";
            ddlRoles.DataValueField = "RoleId";
            ddlRoles.DataBind();

            //featured ads
            this.rptThumnailFeatured.DataSource = this.rptContentFeatured.DataSource = avdd.GetFeaturedAdsDataTable();
            this.rptContentFeatured.DataBind();
            this.rptThumnailFeatured.DataBind();

            //city

            ddlCities.DataSource = cd.getCitiesData();
            ddlCities.DataTextField = "CityName";
            ddlCities.DataValueField = "CityId";
            ddlCities.DataBind();
            ddlCities.Items.Insert(0, new ListItem("---Choose all----", "0"));

            //district
            ddlDistricts.DataSource = dtd.getDistrictsData();
            ddlDistricts.DataTextField = "DistrictName";
            ddlDistricts.DataValueField = "DistrictId";
            ddlDistricts.DataBind();
            ddlDistricts.Items.Insert(0, new ListItem("---Choose all----", "0"));
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (search.Text !=string.Empty)
            {
                Response.Redirect("Search.aspx?type=basic&key=" + search.Text);
            }
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            Account acc = new Account();
            AccountDAL ad = new AccountDAL();
            acc.Role = new Role();
            acc.Role.RoleId = int.Parse(ddlRoles.SelectedValue);
            acc.Username = txtUsername.Text.ToLower();
            acc.Password = Utilities.GetMD5HashData(txtPassword.Text.Trim());
            acc.Email = txtEmail.Text;
            acc.FirstName = txtFirstName.Text;
            acc.LastName = txtLastName.Text;
            acc.PayPalAccount = txtPaypalAccount.Text;
            acc.Company = txtCompany.Text;
            acc.Phone = txtPhone.Text;
            acc.Address = txtAdress.Text;
            acc.IsActive = true;
            acc.RegDate = DateTime.Now.ToShortDateString();
            if (ad.AddAccount(acc))
            {
                //do something
                Session["adminId"] = ad.GetIdByUsername(txtUsername.Text.ToLower());
                Session["adminLogin"] = txtUsername.Text;
                Session["adminRole"] = ddlRoles.Text;

                taskbar_right.Visible = false;
                LoggedPanel.Visible = true;
                lblUserName.Text = txtUsername.Text;
            }
        }

        protected void btnAdvancedSearch_Click(object sender, EventArgs e)
        {
            string propertyType = (rbtnRenting.Checked) ? "Rent" : (rbtnSelling.Checked) ? "Sell" : "OK";
            string city = (ddlCities.SelectedIndex == 0) ? "" : ddlCities.SelectedValue;
            string district = (ddlDistricts.SelectedIndex == 0) ? "" : ddlDistricts.SelectedValue;
            string category = (ddlCategories.SelectedIndex == 0) ? "" : ddlCategories.SelectedValue;
            string areaFrom = (ddlAreaFrom.SelectedIndex == 0) ? "" : ddlAreaFrom.SelectedValue;
            string areaTo = (ddlAreaTo.SelectedIndex == 0) ? "" : ddlAreaTo.SelectedValue;
            string priceFrom =(ddlPriceFrom.SelectedIndex==0)?"" : ddlPriceFrom.SelectedValue;
            string priceTo = (ddlPriceTo.SelectedIndex == 0) ? "" : ddlPriceTo.SelectedValue;

            string url = "Search.aspx?type=advanced&propertyType=" + propertyType;
            if (city != "")
            {
                url += "&city=" + city;
            }
            if (district != "")
            {
                url += "&district=" + district;
            }
            if(category!=""){
                url += "&category=" + category;
            }
            if (areaFrom != "")
            {
                url += "&areafrom=" + areaFrom;
            }
            if (areaTo!="")
            {
                url += "&areato=" + areaTo;
            }
            if (priceFrom != "") {
                url += "&pricefrom=" + priceFrom;
            }
            if (priceTo != "")
            {
                url += "&priceto="+ priceTo;
            }

            Response.Redirect(url);
        }

        protected void ddlCities_SelectedIndexChanged(object sender, EventArgs e)
        {

            string ctId = ddlCities.SelectedValue;
            if (ctId != "0")
            {
                District d = new District();
                d.CityId = int.Parse(ctId);
                DataTable dt = dtd.GetDistrictByCityId(d);
                ddlDistricts.DataSource = dt;
                ddlDistricts.DataTextField = "DistrictName";
                ddlDistricts.DataValueField = "DistrictId";
                ddlDistricts.DataBind();
            }
            else
            {
                ddlDistricts.DataSource = dtd.getDistrictsData();
                ddlDistricts.DataTextField = "DistrictName";
                ddlDistricts.DataValueField = "DistrictId";
                ddlDistricts.DataBind();
                ddlDistricts.Items.Insert(0, new ListItem("---Choose all----", "0"));
            }
        }

    }
}